package com.aispeech.asr.apis.detection.callback;

import java.io.IOException;
import java.util.List;

import com.aispeech.asr.apis.detection.vo.SedVo;
import com.aispeech.asr.apis.detection.websocket.connection.EmitterManager;
import com.aispeech.jarpc.ArpcException;
import com.aispeech.jarpc.IArpcCallback;
import com.aispeech.jarpc.message.ArpcMessage;
import com.alibaba.fastjson.JSONArray;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class SedOnlineCallback implements IArpcCallback {

    private EmitterManager manager;
    private String clientSessionId;
	
    public SedOnlineCallback(String clientSessionId, EmitterManager manager) {
    	this.manager = manager;
    	this.clientSessionId = clientSessionId;
    }
	
	@Override
	public void onArpcMessage(ArpcMessage message) {
		try {
			
			String onArpcMessage = new String(message.getBody(), "utf-8");
			log.info("==========onArpcMessage:{}",onArpcMessage);
			
			List<SedVo> resultList = JSONArray.parseArray(new String(message.getBody(), "utf-8"), SedVo.class);
//			ObjectMapper objectMapper = new ObjectMapper();
//			CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, EventDetection.class);
//	        List<EventDetection> resultList = objectMapper.readValue(message.getBody(), listType);
			
			manager.doResponse(clientSessionId, resultList, 0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			log.error("[SedOnlineCallback] failed sessionId:{}",clientSessionId,e);
		}
	}
	
	@Override
	public void onArpcError(ArpcException exception) {
		
		manager.doErrorResponse(clientSessionId, exception);
		
	}

}
